cmake_minimum_required(VERSION 3.5)
project(diylane)

set(CMAKE_CXX_STANDARD 11)

MESSAGE(STATUS "Current dir: ${CMAKE_CURRENT_SOURCE_DIR}")
string(REPLACE "\\" "/" Home "$ENV{HOMEDRIVE}$ENV{HOMEPATH}")
SET(GithubBase "${Home}/space/repos/github")
if (MSVC)

    MESSAGE(STATUS "GithubBase dir: ${GithubBase}")
    include_directories(
            D:/local/boost_1_64_0/
            D:/local/openssl-OpenSSL_1_1_0e/include/
            D:/local/pgsql_64/include/
            D:/local/libmicrohttpd-0.9.54-w32-bin/x86_64/VS2017/Debug-static/
            D:/local/curl-7.54.0/builds/libcurl-vc14-x86-release-static-ipv6-sspi-winssl/include/
            ${GithubBase}/crow/include/
            ${GithubBase}/
            ${GithubBase}/rapidjson/include/
            ${GithubBase}/date/
            ${GithubBase}/sqlpp11/include/
            ${GithubBase}/sqlpp11-connector-postgresql/include/
            src/
            test/
            test_cpputest/
    )

    # c++11
    set(CMAKE_CXX_FLAGS "/constexpr:depth512 /constexpr:backtrace32 /constexpr:steps100000 /Zc:rvalueCast,auto,implicitNoexcept,sizedDealloc,inline,referenceBinding,forScope,trigraphs,wchar_t ${CMAKE_CXX_FLAGS}")

    set(CMAKE_CXX_FLAGS "/utf-8 /DCROW_MSVC_WORKAROUND /D_WIN32_WINNT=0x0501 /W3 ${CMAKE_CXX_FLAGS}")
    set(CMAKE_CXX_FLAGS "/DNOMINMAX ${CMAKE_CXX_FLAGS}")
    set(CMAKE_CXX_FLAGS "/D_WINDOWS /DWIN64 ${CMAKE_CXX_FLAGS}")

    # 这个没有什么效果
    set(CMAKE_EXE_LINKER_FLAGS "/MACHINE:X64 ${CMAKE_EXE_LINKER_FLAGS}")

    link_directories(
            D:/local/boost_1_64_0/lib64-msvc-14.0
            D:/local/curl-7.54.0/builds/libcurl-vc14-x86-release-static-ipv6-sspi-winssl/lib
            D:/local/libmicrohttpd-0.9.54-w32-bin/x86_64/VS2015/Debug-static
            D:/local/openssl-OpenSSL_1_1_0e
            D:/local/pgsql_64/lib
    )

else ()
    find_package(Boost COMPONENTS filesystem unit_test_framework system thread timer date_time REQUIRED)
    include_directories(
            src/
            test/
            test_cpputest/
    )
endif ()

file(GLOB DataModels "src/business/models/*.h")

set(Headers
        ${DataModels}
        src/modules/loggger/concurrent_queue.hpp
        src/modules/loggger/outputdebugstream.hpp
        src/modules/loggger/cpplog.hpp
        src/modules/loggger/logger.hpp
        src/modules/client.hpp
        src/modules/cookies.hpp
        src/modules/jobs.hpp
        src/modules/postgres.hpp
        src/modules/redis.hpp
        src/modules/server.hpp
        src/modules/utilities.hpp
        src/modules/worker_pool.hpp
        src/modules/middlewares/session.hpp
        src/business/arguments.hpp
        src/business/error.hpp
        src/business/result.hpp
        src/business/sqlpp_aliases.hpp
        src/business/service/session.hpp
        src/business/service/setup.hpp
        src/business/service/user.hpp
        src/business/dao/User.hpp
        src/application.hpp
        )

if (MSVC)
    set(Imported_Libraries
            libmicrohttpd_d
            libcurl_a
            )
else ()

    set(Imported_Libraries
            ${Boost_SYSTEM_LIBRARY}
            ${Boost_THREAD_LIBRARY}
            ${Boost_TIMER_LIBRARY}
            ${Boost_DATE_TIME_LIBRARY}
            ${Boost_FILESYSTEM_LIBRARY}
            ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
            pthread
            microhttpd
            hiredis
            sqlpp-postgresql
            pq
            curl
            ssl
            crypto)

endif ()

if (MSVC)
    set(CompileOptions)    
else ()
    set(CompileOptions -fexceptions -fnon-call-exceptions)
endif ()

if(UNIX)
    set(Imported_Libraries dl ${Imported_Libraries})
endif()

add_executable(diylane
        ${Headers}
        prj/mac/diylane-api/main.cpp)
target_compile_options(diylane PUBLIC ${CompileOptions})
target_link_libraries(diylane ${Imported_Libraries})

##
## diylane_test
## eclipse上需要在工程属性中把__cplusplus设置成201103L,然后重建索引
##
add_executable(diylane_test
        test/test.hpp
        test/test_dao_user.hpp
        test/test_misc_httpclient.hpp
        test/test_service_setup.hpp
        test/test_service_user.hpp
        test/test.cpp)
target_compile_options(diylane_test PUBLIC
        ${CompileOptions})
target_link_libraries(diylane_test
        ${Imported_Libraries})

##
## diylane_cpputest
##
add_executable(diylane_cpputest
        test_cpputest/cpputest.hpp
        test_cpputest/dao/user.cpp
        test_cpputest/misc/httpclient.cpp
        test_cpputest/service/setup.cpp
        test_cpputest/service/user.cpp
        test_cpputest/cpputest.cpp)
target_compile_options(diylane_cpputest PUBLIC
        -include ${CMAKE_CURRENT_SOURCE_DIR}/test_cpputest/prelude.hpp
        ${CompileOptions})
target_link_libraries(diylane_cpputest
        CppUTest
        ${Imported_Libraries})

enable_testing()
add_test(NAME test COMMAND diylane_test)
add_test(NAME cpputest COMMAND diylane_cpputest)
